package com.shd.edanpei.view.picker;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.shd.edanpei.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 预期比
 */
public class RatePicker2 {

    /**
     * 定义结果回调接口
     */
    public interface ResultHandler {
        void handle(String time);
    }

    public enum SCROLL_TYPE {
        HOUR(1),
        MINUTE(2);

        SCROLL_TYPE(int value) {
            this.value = value;
        }

        public int value;
    }

    private int scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
    private ResultHandler handler;
    private Context context;
    private boolean canAccess = false;

    private Dialog datePickerDialog;
    private PickerView picker_rate;

    private final int TYPE_DOUBLE = 1;
    private final int TYPE_INTEGER = 2;


    private TextView tv_cancle, tv_picker_rate_title, tv_select, tv_rate_min, tv_picker_unit1, tv_picker_unit2;

    private List<String> dataList = new ArrayList<>();
    private boolean isDone = false;
    private double DMIN;
    private double mMin;
    private double mMax;
    private double mGains;
    private String mUnit;
    private String mTitle;
    private String mData;

    private int mType;


    /**
     * 操作 Double数据
     */
    public RatePicker2(Context context, ResultHandler resultHandler, double min, double max, double gains, String unit, String title) {
        canAccess = true;
        this.mType = TYPE_DOUBLE;
        this.context = context;
        this.handler = resultHandler;
        this.DMIN = min;
        this.mMin = min;
        this.mMax = max;
        this.mGains = gains;
        this.mUnit = unit;
        this.mTitle = title;


        while (!isDone) {
            mMin = add(mMin, mGains);
            dataList.add(mMin + "");
            if (mMin == mMax) {
                isDone = true;
            }
        }

        initDialog();
        initView();
    }

    private int IMIN;
    private int iMin;
    private int iMax;
    private int iGains;

    /**
     * 操作Integer数据
     */
    public RatePicker2(Context context, ResultHandler resultHandler, int min, int max, int gains, String unit, String title) {
        canAccess = true;
        this.mType = TYPE_INTEGER;
        this.context = context;
        this.handler = resultHandler;
        IMIN = min;
        this.iMin = min;
        this.iMax = max;
        this.iGains = gains;
        this.mUnit = unit;
        this.mTitle = title;


        while (!isDone) {
            iMin = iMin + iGains;
            dataList.add(iMin + "");
            if (iMin == iMax) {
                isDone = true;
            }
        }

        initDialog();
        initView();
    }

    public double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }


    private void initDialog() {
        if (datePickerDialog == null) {
            datePickerDialog = new Dialog(context, R.style.time_dialog);
            datePickerDialog.setCancelable(false);
            datePickerDialog.setCanceledOnTouchOutside(true);
            datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            datePickerDialog.setContentView(R.layout.picker_rate);
            Window window = datePickerDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels;
            window.setAttributes(lp);
        }
    }

    private void initView() {
        picker_rate = datePickerDialog.findViewById(R.id.picker_rate);
        tv_cancle = datePickerDialog.findViewById(R.id.tv_cancle);
        tv_picker_rate_title = datePickerDialog.findViewById(R.id.tv_picker_rate_title);
        tv_picker_unit1 = datePickerDialog.findViewById(R.id.tv_picker_rate_unit1);
        tv_picker_unit2 = datePickerDialog.findViewById(R.id.tv_picker_rate_unit2);
        tv_select = datePickerDialog.findViewById(R.id.tv_select);
        tv_rate_min = datePickerDialog.findViewById(R.id.tv_picker_rate_min);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.dismiss();
            }
        });

        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(mData)) {
                    handler.handle(mData);
                }
                datePickerDialog.dismiss();
            }
        });
    }

    private void initTimer() {


    }


    private void loadComponent() {
        picker_rate.setData(dataList);
        tv_picker_rate_title.setText(mTitle);
        switch (mType) {
            case TYPE_DOUBLE:
                tv_rate_min.setText(DMIN + "");
                break;
            case TYPE_INTEGER:
                tv_rate_min.setText(IMIN + "");
                break;
        }
        tv_picker_unit1.setText(mUnit);
        tv_picker_unit2.setText(mUnit);
        picker_rate.setIsLoop(false);
        if (dataList != null && dataList.size() > 0) {
            mData = dataList.get(0);
        }
        picker_rate.setSelected(0);
        executeScroll();
    }

    private void addListener() {
        picker_rate.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mData = text;
            }
        });

    }

    private void executeScroll() {
        picker_rate.setCanScroll(dataList.size() > 1);
    }

    private int disScrollUnit(SCROLL_TYPE... scroll_types) {
        if (scroll_types == null || scroll_types.length == 0) {
            scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
        } else {
            for (SCROLL_TYPE scroll_type : scroll_types) {
                scrollUnits ^= scroll_type.value;
            }
        }
        return scrollUnits;
    }

    public void show() {
        if (canAccess) {
            canAccess = true;
            loadComponent();
            addListener();
//            setSelectedTime(time);
            datePickerDialog.show();
        }
    }


    /**
     * 设置日期控件默认选中的时间
     */
    public void setSelectedTime(String time) {

    }

}
